// subPages/pensionService/kitchenDetails/kitchenDetails.js
const app = getApp();
var util = require('../../../utils/util');
Page({

  /**
   * 页面的初始数据
   */
  data: {
    themeColor:app.globalData.themeColor
  },
  addShoppingcart(){
    var _this = this
    if(app.globalData.userId){
        price,num =1,
        id = _this.data.goodsId,
        shoppingcartList = _this.data.shoppingcartList;
        for(var j in shoppingcartList){
            if(id == shoppingcartList[j].product_id){
                num = ++shoppingcartList[j].num
            }
        }

        _this.getShoppingcartList()
        util.getAjax(`${app.globalData.apiUrl}/operation_community_service_kitchen_product_buycar`,res=>{
            console.log(res);
            if(res.data.status == 1){
            util.showToast('购物车添加成功！','none',2000);
            }
            _this.getShoppingcartList()
        },{
            type:1,
            kitchen_id:_this.data.kitchenId,
            product_id:id,
            user_id:app.globalData.userId,
            num:num,
            price:_this.data.kitchenInfo.price,
        })
    }else{
        util.showModelAll('提示','该功能需要授权登录后使用，请进行授权登录',function(){
            if (app.globalData.userInfo) {
                //已授权
                wx.redirectTo({
                    url: '/pages/login/login'
                })
            }else{
                // 在没有 open-type=getUserInfo 版本的兼容处理
                if(app.globalData.isAuthorized){
                    app.userInfoReadyCallback = res => {
                      if (res) {
                        //已授权
                        wx.redirectTo({
                          url: '/pages/login/login'
                        })
                      }else{
                        //未授权
                        wx.navigateTo({
                          url: '/pages/authorizedLogin/authorizedLogin?isLogin='+ false,
                        })
                      }
                    }
                }else{
                    wx.navigateTo({
                      url: '/pages/authorizedLogin/authorizedLogin?isLogin='+ false,
                    })
                }
            }
        })
    }
  },
    buyNow(){
        var _this = this;
        if(app.globalData.userId){
            wx.navigateTo({
                url:'/subPages/pensionService/confirmOrder/confirmOrder?type=1&id='+_this.data.goodsId
            })
        }else{
            util.showModelAll('提示','该功能需要授权登录后使用，请进行授权登录',function(){
                if (app.globalData.userInfo) {
                    //已授权
                    wx.redirectTo({
                        url: '/pages/login/login'
                    })
                }else{
                    // 在没有 open-type=getUserInfo 版本的兼容处理
                    if(app.globalData.isAuthorized){
                        app.userInfoReadyCallback = res => {
                          if (res) {
                            //已授权
                            wx.redirectTo({
                              url: '/pages/login/login'
                            })
                          }else{
                            //未授权
                            wx.navigateTo({
                              url: '/pages/authorizedLogin/authorizedLogin?isLogin='+ false,
                            })
                          }
                        }
                    }else{
                        wx.navigateTo({
                          url: '/pages/authorizedLogin/authorizedLogin?isLogin='+ false,
                        })
                    }
                }
            })
        }
      
    },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    let id = options.id;
    let type = options.type;
    this.setData({
        goodsId:options.id,
        type:type
    })
    util.setNavBarTitle('详情');
    this.getKitchenDetails(id);
    this.getKitchen();
  },
  getKitchenDetails(id){
    let _this = this;
    util.getAjax(`${app.globalData.apiUrl}/get_community_service_kitchen_product_content`,res=>{
        console.log(res);
        _this.setData({
            kitchenInfo:res.data
        })
    },{
      id:id
    })
  },
    getKitchen(){
        let _this = this;
        util.getAjax(`${app.globalData.apiUrl}/get_community_service_kitchen`,res=>{
            console.log(res);
            _this.setData({
                kitchenId:res.data.id
            })
            // _this.getKitchenClass(_this.data.type),
                _this.getShoppingcartList();
        },{
            community_id:app.globalData.communityId
        })
    },
    //获取购物车
    getShoppingcartList(){
        let _this = this;
        // let shoppingcartList=_this.data.shoppingcartList;
        util.getAjax(`${app.globalData.apiUrl}/get_community_service_kitchen_buycar`,res=>{
            console.log('shoppingcart',res);
            //计算总数
            let money = 0;
            for(var i in res.data){
                money += res.data[i].num * res.data[i].price
            }
            _this.setData({
                shoppingcartList:res.data,
                shoppingcartNum:res.data.length,
                totalMoney:money
            })
        },{
            user_id:app.globalData.userId,
            kitchen_id:_this.data.kitchenId
        })
    },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})
